/**
 * Project Name:YCApiServer-Api
 * File Name:PushService.java
 * Package Name:com.yc.music.service.server22
 * Date:2016年9月10日下午4:27:27
 * Copyright (c) 2016 
 *
 */
package com.yc.music.service.server22;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.yc.music.common.context.YcContext;
import com.yc.music.common.push.YcPush;
import com.yc.music.context.YtConstant;
import com.yc.music.mapper.cache.UserCacheMapper;
import com.yc.music.mapper.cache.WorkCacheMapper;
import com.yc.music.mapper.dao.CooperateWorkMapper;
import com.yc.music.mapper.dao.LyricsMapper;
import com.yc.music.mapper.dao.PushMessageFlowMapper;
import com.yc.music.model.YcPushMassageFlow;
import com.yc.music.model.cooperate.CooperateWorkAccessVo;
import com.yc.music.model.cooperate.CooperateWorkVo;
import com.yc.music.model.musicLyrics.MusicLyrics;
import com.yc.music.model.musicWork.WorkOpenDetail;
import com.yc.music.model.musicuser.MusicUserVo;

import cn.jpush.api.push.PushResult;

/**
 * ClassName: PushService <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2016年9月10日 下午4:27:27 <br/>
 *
 * @author qinhaiguan
 * @version v2.2
 */
@Service
public class PushService {
	
	private static Logger logger = Logger.getLogger(PushService.class);
	
	@Autowired
	private WorkCacheMapper workCacheMapper;
	
	@Autowired
	private LyricsMapper lyricsMapper;
	
	@Autowired
	private PushMessageFlowMapper pushMessageFlowMapper;
	
	@Autowired
	private UserCacheMapper userCacheMapper;
	
	@Autowired
	private CooperateWorkMapper cooperateWorkMapper;
	

	/**
	 * 
	 * addWorkToSonglist:(添加歌曲到歌单). <br/> 
	 *
	 * @author qinhaiguan
	 * @param uid  用户id
	 * @param songListName  歌单名
	 * @param workid   歌曲id
	 * @param result
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public Map<String,Object> addWorkToSonglist(Long uid,String songListName,Long workid,Map<String,Object> result) throws Exception {
		
		WorkOpenDetail work=workCacheMapper.getWorOpenDetailById(workid);
		
		
		//***********增加推送部分****************
		
		logger.info("开始推送歌曲作品被添加至歌单消息>>>>>>推送给被关注者用户id:"+uid+",歌单名:"+songListName+",作品名："+work.getTitle());
		
		//针对一个人一个作品只第一次的时候推送
		int pushCount=pushMessageFlowMapper.getCountPushMessage(null,uid, workid, 1, YtConstant.PUSH_TYPE_ADDTOSONGLIST);
		String msgIdAnd=null;
		String msgIdIOS=null;
		YcPushMassageFlow flow =new YcPushMassageFlow();
		if (0==pushCount) {
			Map<String,Object> param = new HashMap<String,Object>();
			
			param.put("content", YtConstant.PUSH_TITLE_ADDTOSONGLIST.replaceAll("W", work.getTitle()).replaceAll("S", songListName));
			param.put("type", YtConstant.PUSH_TYPE_ADDTOSONGLIST);
			param.put("title", YtConstant.YINCHAO);
			String pushMessage=JSONObject.toJSONString(param);
			PushResult reAnd=YcPush.ycSendPushAndroid(String.valueOf(uid), pushMessage);
			logger.info("推送作品被添加至歌单消息返回结果Android:"+reAnd);
			PushResult reIOS=YcPush.ycSendPushIOS(String.valueOf(uid), pushMessage);
			logger.info("推送作品被添加至歌单消息返回结果IOS:"+reIOS);
			if (null!=reAnd) {
				msgIdAnd=JSONObject.parseObject(JSONObject.toJSONString(reAnd)).getString(YtConstant.JPUSH_KEY_MSGID);
				flow.setPlatform(YtConstant.JPUSH_PLATFORM_ANDROID);
				flow.setMsgid(msgIdAnd);
			}else if(null!=reIOS){
				msgIdIOS=JSONObject.parseObject(JSONObject.toJSONString(reIOS)).getString(YtConstant.JPUSH_KEY_MSGID);
				flow.setPlatform(YtConstant.JPUSH_PLATFORM_IOS);
				flow.setMsgid(msgIdIOS);
			}else{
				flow.setPlatform(YtConstant.JPUSH_PLATFORM_NULL);
				flow.setMsgid(YtConstant.JPUSH_KEY_MSGID_NULL);
			}
			
		}
//		String msgId="66666666";
		//记录推送流水
		flow.setOtheruid(uid);
		flow.setWorkid(workid);
		flow.setWorktype(1);
		flow.setPushtype(YtConstant.PUSH_TYPE_ADDTOSONGLIST);
		flow.setContent(YtConstant.PUSH_TITLE_ADDTOSONGLIST.replaceAll("W", work.getTitle()).replaceAll("S", songListName));//保存推送内容
		
		pushMessageFlowMapper.savePushMessageFlow(flow);
		
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YcContext.EMPTY_STRING);
		result.put(YcContext.DATA, "");
		return result;
	}
	
	/**
	 * 
	 * addCooperateworkTosonglist23:(添加合作作品到歌单). <br/> 
	 *
	 * @author qinhaiguan
	 * @param wuid
	 * @param luid
	 * @param songListName
	 * @param workid
	 * @param result
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	 
	public Map<String,Object> addCooperateworkTosonglist23(Long wuid,Long luid,String songListName,Long workid,Map<String,Object> result) throws Exception {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("type", YtConstant.PUSH_TYPE_ADDTOSONGLIST);
		param.put("title", YtConstant.YINCHAO);
		//记录推送流水
		YcPushMassageFlow flow =new YcPushMassageFlow();
		
		CooperateWorkVo cooperateWorkVo=cooperateWorkMapper.getCooperateWorkById(workid);
		
	
		String msgIdAnd=null;
		String msgIdIOS=null;
		
		CooperateWorkAccessVo accessVo=cooperateWorkMapper.getCooperateWorkAccessById(workid);
		if (null!=accessVo) {//合作歌曲作品已经被采纳,同时推送给词作者、曲作者【若一方删除，则不推送给此人】
			long wUid=accessVo.getwUid();
			//查询作曲用户是否已经删除作品
			int wCount=cooperateWorkMapper.getCooperateWorkDelete(workid, wUid);
			if (wCount==0) {
//				//通过用户id获取用户名
//				MusicUserVo userVo=userCacheMapper.getCacheUservo(wUid);
				param.put("content", YtConstant.PUSH_TITLE_ADDTOSONGLIST.replaceAll("W", cooperateWorkVo.getTitle()).replaceAll("S", songListName));
				String pushMessage=JSONObject.toJSONString(param);
				logger.info("合作歌曲开始推荐到歌单消息>>>>>>推送给用户id:"+wUid+",歌单名:"+songListName+",歌曲名:"+cooperateWorkVo.getTitle());
				PushResult reAnd=YcPush.ycSendPushAndroid(String.valueOf(wUid), pushMessage);
				logger.info("合作歌曲推送推荐到歌单消息返回结果Android:"+reAnd);
				PushResult reIOS=YcPush.ycSendPushIOS(String.valueOf(wUid), pushMessage);
				logger.info("合作歌曲推送推荐到歌单消息返回结果IOS:"+reIOS);
				
				//流水部分
				if (null!=reAnd) {
					msgIdAnd=JSONObject.parseObject(JSONObject.toJSONString(reAnd)).getString(YtConstant.JPUSH_KEY_MSGID);
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_ANDROID);
					flow.setMsgid(msgIdAnd);
				}else if(null!=reIOS){
					msgIdIOS=JSONObject.parseObject(JSONObject.toJSONString(reIOS)).getString(YtConstant.JPUSH_KEY_MSGID);
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_IOS);
					flow.setMsgid(msgIdIOS);
				}else{
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_NULL);
					flow.setMsgid(YtConstant.JPUSH_KEY_MSGID_NULL);
				}
				flow.setOtheruid(wUid);
			}
			long lUid=accessVo.getlUid();
			//查询作词用户是否已经删除作品
			int lCount=cooperateWorkMapper.getCooperateWorkDelete(workid, lUid);
			if (lCount==0) {
//				//通过用户id获取用户名
//				MusicUserVo userVo=userCacheMapper.getCacheUservo(wUid);
				param.put("content", YtConstant.PUSH_TITLE_ADDTOSONGLIST.replaceAll("W", cooperateWorkVo.getTitle()).replaceAll("S", songListName));
				String pushMessage=JSONObject.toJSONString(param);
				logger.info("合作歌曲开始推荐到歌单消息>>>>>>推送给用户id:"+lUid+",歌单名:"+songListName+",歌曲名:"+cooperateWorkVo.getTitle());
				PushResult reAnd=YcPush.ycSendPushAndroid(String.valueOf(lUid), pushMessage);
				logger.info("合作歌曲推送推荐到歌单消息返回结果Android:"+reAnd);
				PushResult reIOS=YcPush.ycSendPushIOS(String.valueOf(lUid), pushMessage);
				logger.info("合作歌曲推送推荐到歌单消息返回结果IOS:"+reIOS);
				
				//流水部分
				if (null!=reAnd) {
					msgIdAnd=JSONObject.parseObject(JSONObject.toJSONString(reAnd)).getString(YtConstant.JPUSH_KEY_MSGID);
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_ANDROID);
					flow.setMsgid(msgIdAnd);
				}else if(null!=reIOS){
					msgIdIOS=JSONObject.parseObject(JSONObject.toJSONString(reIOS)).getString(YtConstant.JPUSH_KEY_MSGID);
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_IOS);
					flow.setMsgid(msgIdIOS);
				}else{
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_NULL);
					flow.setMsgid(YtConstant.JPUSH_KEY_MSGID_NULL);
				}
				flow.setOtheruid(lUid);
			}
		}else{//合作歌曲作品未被采纳，只推送给曲作者
			CooperateWorkAccessVo notAccessVo=cooperateWorkMapper.getCooperateWorkNotAccessById(workid,new Date());
			if (null!=notAccessVo) {
				long oneUid=notAccessVo.getwUid();
				param.put("content", YtConstant.PUSH_TITLE_ADDTOSONGLIST.replaceAll("W", cooperateWorkVo.getTitle()).replaceAll("S", songListName));
				String pushMessage=JSONObject.toJSONString(param);
				logger.info("合作歌曲开始推荐到歌单消息>>>>>>推送给用户id:"+oneUid+",歌单名:"+songListName+",歌曲名:"+cooperateWorkVo.getTitle());
				PushResult reAnd=YcPush.ycSendPushAndroid(String.valueOf(oneUid), pushMessage);
				logger.info("合作歌曲推送推荐到歌单消息返回结果Android:"+reAnd);
				PushResult reIOS=YcPush.ycSendPushIOS(String.valueOf(oneUid), pushMessage);
				logger.info("合作歌曲推送推荐到歌单消息返回结果IOS:"+reIOS);
				
				//流水部分
				if (null!=reAnd) {
					msgIdAnd=JSONObject.parseObject(JSONObject.toJSONString(reAnd)).getString(YtConstant.JPUSH_KEY_MSGID);
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_ANDROID);
					flow.setMsgid(msgIdAnd);
				}else if(null!=reIOS){
					msgIdIOS=JSONObject.parseObject(JSONObject.toJSONString(reIOS)).getString(YtConstant.JPUSH_KEY_MSGID);
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_IOS);
					flow.setMsgid(msgIdIOS);
				}else{
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_NULL);
					flow.setMsgid(YtConstant.JPUSH_KEY_MSGID_NULL);
				}
				flow.setOtheruid(oneUid);
			}
		}
		
		flow.setWorktype(3);
		flow.setWorkid(workid);
		flow.setContent(YtConstant.PUSH_TITLE_ADDTOSONGLIST.replaceAll("W", cooperateWorkVo.getTitle()).replaceAll("S", songListName));
		flow.setPushtype(YtConstant.PUSH_TYPE_ADDTOSONGLIST);
		pushMessageFlowMapper.savePushMessageFlow(flow);
		
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YcContext.EMPTY_STRING);
		result.put(YcContext.DATA, "");
		return result;
	}
	
	/**
	 * 
	 * recommedWorkToIndex:(推送作品到首页). <br/> 
	 *
	 * @author qinhaiguan
	 * @param uid 用户id
	 * @param workid  作品id
	 * @param type 作品类型
	 * @param result
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public Map<String,Object> recommedWorkToIndex(Long uid,Long workid,Integer type,Map<String,Object> result) throws Exception {
		
		//***********增加推送部分****************
		//1歌曲 2歌词
		if (1==type) {
			WorkOpenDetail work=workCacheMapper.getWorOpenDetailById(workid);
			logger.info("开始推送歌曲作品被推送到首页消息>>>>>>推送给用户id:"+uid+",作品名："+work.getTitle());
			//针对一个人一个作品只第一次的时候推送
			int pushCount=pushMessageFlowMapper.getCountPushMessage(null,uid, workid, 1, YtConstant.PUSH_TYPE_RECOMENDTOINDEX);
			String msgIdAnd=null;
			String msgIdIOS=null;
			YcPushMassageFlow flow =new YcPushMassageFlow();
			if (0==pushCount) {
				//推送给android平台
				msgIdAnd=this.pushRecommedAndroid(String.valueOf(uid), work.getTitle());
				if (null!=msgIdAnd) {
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_ANDROID);
					flow.setMsgid(msgIdAnd);
				}
				//推送给IOS平台
				msgIdIOS=this.pushRecommedIOS(String.valueOf(uid), work.getTitle());
				if (null!=msgIdIOS) {
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_IOS);
					flow.setMsgid(msgIdIOS);
				}
				
				if (null==msgIdAnd&&null==msgIdIOS) {
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_NULL);
					flow.setMsgid(YtConstant.JPUSH_KEY_MSGID_NULL);
					
				}
			}

//			String msgId="55555555";
			//记录推送流水
			
			flow.setOtheruid(uid);
			flow.setWorkid(workid);
			flow.setWorktype(type);//1歌曲 2歌词
			flow.setPushtype(YtConstant.PUSH_TYPE_RECOMENDTOINDEX);
			flow.setContent(YtConstant.PUSH_TITLE_RECOMENDTOINDEX.replaceAll("W", work.getTitle()));
			pushMessageFlowMapper.savePushMessageFlow(flow);
		}else{
			MusicLyrics  lyrics=lyricsMapper.getShareLyricById(workid);
			logger.info("开始推送歌词作品被推送到首页消息>>>>>>推送给用户id:"+uid+",作品名："+lyrics.getTitle());
			//针对一个人一个作品只第一次的时候推送
			int pushCount=pushMessageFlowMapper.getCountPushMessage(null,uid, workid, 2, YtConstant.PUSH_TYPE_RECOMENDTOINDEX);
			String msgIdAnd=null;
			String msgIdIOS=null;
			YcPushMassageFlow flow =new YcPushMassageFlow();
			if (0==pushCount) {
				//推送给android平台
				msgIdAnd=this.pushRecommedAndroid(String.valueOf(uid), lyrics.getTitle());
				if (null!=msgIdAnd) {
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_ANDROID);
					flow.setMsgid(msgIdAnd);
				}
				//推送给IOS平台
				msgIdIOS=this.pushRecommedIOS(String.valueOf(uid), lyrics.getTitle());
				if (null!=msgIdIOS) {
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_IOS);
					flow.setMsgid(msgIdIOS);
				}
				
				if (null==msgIdAnd&&null==msgIdIOS) {
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_NULL);
					flow.setMsgid(YtConstant.JPUSH_KEY_MSGID_NULL);
					
				}
			}

//			String msgId="55555555";
			//记录推送流水
			flow.setOtheruid(uid);
			flow.setWorkid(workid);
			flow.setWorktype(type);//1歌曲 2歌词
			flow.setPushtype(YtConstant.PUSH_TYPE_RECOMENDTOINDEX);
			
			flow.setContent(YtConstant.PUSH_TITLE_RECOMENDTOINDEX.replaceAll("W", lyrics.getTitle()));
			pushMessageFlowMapper.savePushMessageFlow(flow);
		}
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YcContext.EMPTY_STRING);
		result.put(YcContext.DATA, "");
		return result;
	}
	public Map<String,Object> recommedCooperatewookToindex23(Long wuid,Long luid,Long workid,Integer type,Map<String,Object> result) throws Exception {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("type", YtConstant.PUSH_TYPE_RECOMENDTOINDEX);
		param.put("title", YtConstant.YINCHAO);
		//记录推送流水
		YcPushMassageFlow flow =new YcPushMassageFlow();
		
		CooperateWorkVo cooperateWorkVo=cooperateWorkMapper.getCooperateWorkById(workid);
		
	
		String msgIdAnd=null;
		String msgIdIOS=null;
		
		CooperateWorkAccessVo accessVo=cooperateWorkMapper.getCooperateWorkAccessById(workid);
		if (null!=accessVo) {//合作歌曲作品已经被采纳,同时推送给词作者、曲作者【若一方删除，则不推送给此人】
			long wUid=accessVo.getwUid();
			//查询作曲用户是否已经删除作品
			int wCount=cooperateWorkMapper.getCooperateWorkDelete(workid, wUid);
			if (wCount==0) {
//				//通过用户id获取用户名
//				MusicUserVo userVo=userCacheMapper.getCacheUservo(wUid);
				param.put("content", YtConstant.PUSH_TITLE_RECOMENDTOINDEX.replaceAll("W", cooperateWorkVo.getTitle()));
				String pushMessage=JSONObject.toJSONString(param);
				logger.info("合作歌曲推荐到首页消息>>>>>>推送给用户id:"+wUid+",歌曲名:"+cooperateWorkVo.getTitle());
				PushResult reAnd=YcPush.ycSendPushAndroid(String.valueOf(wUid), pushMessage);
				logger.info("合作歌曲推送推荐到首页消息返回结果Android:"+reAnd);
				PushResult reIOS=YcPush.ycSendPushIOS(String.valueOf(wUid), pushMessage);
				logger.info("合作歌曲推送推荐到首页消息返回结果IOS:"+reIOS);
				
				//流水部分
				if (null!=reAnd) {
					msgIdAnd=JSONObject.parseObject(JSONObject.toJSONString(reAnd)).getString(YtConstant.JPUSH_KEY_MSGID);
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_ANDROID);
					flow.setMsgid(msgIdAnd);
				}else if(null!=reIOS){
					msgIdIOS=JSONObject.parseObject(JSONObject.toJSONString(reIOS)).getString(YtConstant.JPUSH_KEY_MSGID);
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_IOS);
					flow.setMsgid(msgIdIOS);
				}else{
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_NULL);
					flow.setMsgid(YtConstant.JPUSH_KEY_MSGID_NULL);
				}
				flow.setOtheruid(wUid);
			}
			long lUid=accessVo.getlUid();
			//查询作词用户是否已经删除作品
			int lCount=cooperateWorkMapper.getCooperateWorkDelete(workid, lUid);
			if (lCount==0) {
//				//通过用户id获取用户名
//				MusicUserVo userVo=userCacheMapper.getCacheUservo(wUid);
				param.put("content", YtConstant.PUSH_TITLE_RECOMENDTOINDEX.replaceAll("W", cooperateWorkVo.getTitle()));
				String pushMessage=JSONObject.toJSONString(param);
				logger.info("合作歌曲开始推荐到歌单消息>>>>>>推送给用户id:"+lUid+",歌曲名:"+cooperateWorkVo.getTitle());
				PushResult reAnd=YcPush.ycSendPushAndroid(String.valueOf(lUid), pushMessage);
				logger.info("合作歌曲推送推荐到首页消息返回结果Android:"+reAnd);
				PushResult reIOS=YcPush.ycSendPushIOS(String.valueOf(lUid), pushMessage);
				logger.info("合作歌曲推送推荐到首页消息返回结果IOS:"+reIOS);
				
				//流水部分
				if (null!=reAnd) {
					msgIdAnd=JSONObject.parseObject(JSONObject.toJSONString(reAnd)).getString(YtConstant.JPUSH_KEY_MSGID);
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_ANDROID);
					flow.setMsgid(msgIdAnd);
				}else if(null!=reIOS){
					msgIdIOS=JSONObject.parseObject(JSONObject.toJSONString(reIOS)).getString(YtConstant.JPUSH_KEY_MSGID);
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_IOS);
					flow.setMsgid(msgIdIOS);
				}else{
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_NULL);
					flow.setMsgid(YtConstant.JPUSH_KEY_MSGID_NULL);
				}
				flow.setOtheruid(lUid);
			}
		}else{//合作歌曲作品未被采纳，只推送给曲作者
			CooperateWorkAccessVo notAccessVo=cooperateWorkMapper.getCooperateWorkNotAccessById(workid,new Date());
			if (null!=notAccessVo) {
				long oneUid=notAccessVo.getwUid();
				param.put("content", YtConstant.PUSH_TITLE_RECOMENDTOINDEX.replaceAll("W", cooperateWorkVo.getTitle()));
				String pushMessage=JSONObject.toJSONString(param);
				logger.info("合作歌曲推荐到首页消息>>>>>>推送给用户id:"+oneUid+",歌曲名:"+cooperateWorkVo.getTitle());
				PushResult reAnd=YcPush.ycSendPushAndroid(String.valueOf(oneUid), pushMessage);
				logger.info("合作歌曲推送推荐到首页消息返回结果Android:"+reAnd);
				PushResult reIOS=YcPush.ycSendPushIOS(String.valueOf(oneUid), pushMessage);
				logger.info("合作歌曲推送推荐到首页消息返回结果IOS:"+reIOS);
				
				//流水部分
				if (null!=reAnd) {
					msgIdAnd=JSONObject.parseObject(JSONObject.toJSONString(reAnd)).getString(YtConstant.JPUSH_KEY_MSGID);
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_ANDROID);
					flow.setMsgid(msgIdAnd);
				}else if(null!=reIOS){
					msgIdIOS=JSONObject.parseObject(JSONObject.toJSONString(reIOS)).getString(YtConstant.JPUSH_KEY_MSGID);
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_IOS);
					flow.setMsgid(msgIdIOS);
				}else{
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_NULL);
					flow.setMsgid(YtConstant.JPUSH_KEY_MSGID_NULL);
				}
				flow.setOtheruid(oneUid);
			}
		}
		
		flow.setWorktype(3);
		flow.setWorkid(workid);
		flow.setContent(YtConstant.PUSH_TITLE_RECOMENDTOINDEX.replaceAll("W", cooperateWorkVo.getTitle()));
		flow.setPushtype(YtConstant.PUSH_TYPE_RECOMENDTOINDEX);
		pushMessageFlowMapper.savePushMessageFlow(flow);
		
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YcContext.EMPTY_STRING);
		result.put(YcContext.DATA, "");
		return result;
	}

	
	private String pushRecommedAndroid(String uid,String title){
		logger.info("开始推送推荐作品到首页消息Android>>>>>>推送给用户id:"+uid+",作品名:"+title);
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("type", YtConstant.PUSH_TYPE_RECOMENDTOINDEX);
		param.put("title",YtConstant.YINCHAO);
		param.put("content",YtConstant.PUSH_TITLE_RECOMENDTOINDEX.replaceAll("W", title));
		String pushMessage=JSONObject.toJSONString(param);
		PushResult re=YcPush.ycSendPushAndroid(uid, pushMessage);
		logger.info("推送推荐作品到首页消息返回结果Android:"+re);
		String msgId=null;
		if (null!=re) {
			msgId=JSONObject.parseObject(JSONObject.toJSONString(re)).getString(YtConstant.JPUSH_KEY_MSGID);
		}
		return msgId;
	}
	
	private String pushRecommedIOS(String uid,String title){
		logger.info("开始推送推荐作品到首页消息IOS>>>>>>推送给用户id:"+uid+",作品名:"+title);
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("type", YtConstant.PUSH_TYPE_RECOMENDTOINDEX);
		param.put("title",YtConstant.YINCHAO);
		param.put("content",YtConstant.PUSH_TITLE_RECOMENDTOINDEX.replaceAll("W", title));
		String pushMessage=JSONObject.toJSONString(param);
		PushResult re=YcPush.ycSendPushIOS(uid, pushMessage);
		logger.info("推送推荐作品到首页消息返回结果IOS:"+re);
		String msgId=null;
		if (null!=re) {
			msgId=JSONObject.parseObject(JSONObject.toJSONString(re)).getString(YtConstant.JPUSH_KEY_MSGID);
		}
		return msgId;
	}
	
	
	private String pushCopyrightSuccessIOS(String uid,String title,String orderNo){
		logger.info("开始推送作品保权成功消息IOS>>>>>>推送给用户id:"+uid+",作品名:"+title+",订单号："+orderNo);
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("type", YtConstant.PUSH_TYPE_COPYRIGHSUCCESS);
		param.put("orderNo",orderNo);
		param.put("title",YtConstant.YINCHAO);
		param.put("content",YtConstant.PUSH_TITLE_COPYRIGHSUCCESS.replaceAll("W", title));
		String pushMessage=JSONObject.toJSONString(param);
		PushResult re=YcPush.ycSendPushIOS(uid, pushMessage);
		logger.info("推送作品保权成功消息返回结果IOS:"+re);
		String msgId=null;
		if (null!=re) {
			msgId=JSONObject.parseObject(JSONObject.toJSONString(re)).getString(YtConstant.JPUSH_KEY_MSGID);
		}
		return msgId;
	}
	
	
	private String pushCopyrightFailIOS(String uid,String title,String orderNo){
		logger.info("开始推送作品保权失败消息IOS>>>>>>推送给用户id:"+uid+",作品名:"+title+",订单号："+orderNo);
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("type", YtConstant.PUSH_TYPE_COPYRIGHFAIL);
		param.put("orderNo",orderNo);
		param.put("title",YtConstant.YINCHAO);
		param.put("content",YtConstant.PUSH_TITLE_COPYRIGHFAIL.replaceAll("W", title));
		String pushMessage=JSONObject.toJSONString(param);
		PushResult re=YcPush.ycSendPushIOS(uid, pushMessage);
		logger.info("推送保权失败消息返回结果IOS:"+re);
		String msgId=null;
		if (null!=re) {
			msgId=JSONObject.parseObject(JSONObject.toJSONString(re)).getString(YtConstant.JPUSH_KEY_MSGID);
		}
		return msgId;
	}
	
	/**
	 * newActivity:(推送活动上线消息). <br/> 
	 *
	 * @author qinhaiguan
	 * @param title 活动名称
	 * @param result
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public Map<String,Object> newActivity(Long aid,String title,Map<String,Object> result) throws Exception {
		
		//***********增加推送部分****************
		logger.info("开始推送活动上线消息>>>>>>活动名:"+title);
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("type", YtConstant.PUSH_TYPE_NEWACTIVITY);
		param.put("title",YtConstant.YINCHAO);
		param.put("content",YtConstant.PUSH_TITLE_NEWACTIVITY.replaceAll("A", title));
		String pushMessage=JSONObject.toJSONString(param);
		
		String msgIdAnd=null;
		String msgIdIOS=null;
		YcPushMassageFlow flow =new YcPushMassageFlow();
		
		PushResult reAnd=YcPush.ycSendPushAndroid(pushMessage);
		logger.info("推送活动上线消息返回结果Android:"+reAnd);
		PushResult reIOS=YcPush.ycSendPushIOS(pushMessage);
		logger.info("推送活动上线消息返回结果IOS:"+reIOS);
		if (null!=reAnd) {
			msgIdAnd=JSONObject.parseObject(JSONObject.toJSONString(reAnd)).getString(YtConstant.JPUSH_KEY_MSGID);
			flow.setPlatform(YtConstant.JPUSH_PLATFORM_ANDROID);
			flow.setMsgid(msgIdAnd);
		}else if(null!=reIOS){
			msgIdIOS=JSONObject.parseObject(JSONObject.toJSONString(reIOS)).getString(YtConstant.JPUSH_KEY_MSGID);
			flow.setPlatform(YtConstant.JPUSH_PLATFORM_IOS);
			flow.setMsgid(msgIdIOS);
		}else{
			flow.setPlatform(YtConstant.JPUSH_PLATFORM_NULL);
			flow.setMsgid(YtConstant.JPUSH_KEY_MSGID_NULL);
		}
		
		flow.setWorkid(aid);  //活动id
		flow.setContent(title); //活动标题
		flow.setPushtype(YtConstant.PUSH_TYPE_NEWACTIVITY);
		flow.setContent(YtConstant.PUSH_TITLE_NEWACTIVITY.replaceAll("A", title));
		pushMessageFlowMapper.savePushMessageFlow(flow);
		
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YcContext.EMPTY_STRING);
		result.put(YcContext.DATA, "");
		return result;
	}
	
	
	/**
	 * activityFinish:(推送活动结束). <br/> 
	 *
	 * @author qinhaiguan
	 * @param title  活动名称
	 * @param result
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public Map<String,Object> activityFinish(Long aid,String title,Map<String,Object> result) throws Exception {
		
		//***********增加推送部分****************
		logger.info("开始推送活动结束消息>>>>>>活动名:"+title);
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("type", YtConstant.PUSH_TYPE_ACTIVITYFINISH);
		param.put("title",YtConstant.YINCHAO);
		param.put("content",YtConstant.PUSH_TITLE_ACTIVITYFINISH.replaceAll("A", title));
		String pushMessage=JSONObject.toJSONString(param);
		
		String msgIdAnd=null;
		String msgIdIOS=null;
		YcPushMassageFlow flow =new YcPushMassageFlow();
		
		PushResult reAnd=YcPush.ycSendPushAndroid(pushMessage);
		logger.info("推送活动上线消息返回结果Android:"+reAnd);
		PushResult reIOS=YcPush.ycSendPushIOS(pushMessage);
		logger.info("推送活动上线消息返回结果IOS:"+reIOS);
		if (null!=reAnd) {
			msgIdAnd=JSONObject.parseObject(JSONObject.toJSONString(reAnd)).getString(YtConstant.JPUSH_KEY_MSGID);
			flow.setPlatform(YtConstant.JPUSH_PLATFORM_ANDROID);
			flow.setMsgid(msgIdAnd);
		}else if(null!=reIOS){
			msgIdIOS=JSONObject.parseObject(JSONObject.toJSONString(reIOS)).getString(YtConstant.JPUSH_KEY_MSGID);
			flow.setPlatform(YtConstant.JPUSH_PLATFORM_IOS);
			flow.setMsgid(msgIdIOS);
		}else{
			flow.setPlatform(YtConstant.JPUSH_PLATFORM_NULL);
			flow.setMsgid(YtConstant.JPUSH_KEY_MSGID_NULL);
		}
		
		flow.setWorkid(aid);  //活动id
		flow.setContent(title); //活动标题
		flow.setPushtype(YtConstant.PUSH_TYPE_ACTIVITYFINISH);
		flow.setContent(YtConstant.PUSH_TITLE_ACTIVITYFINISH.replaceAll("A", title));
		pushMessageFlowMapper.savePushMessageFlow(flow);
		
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YcContext.EMPTY_STRING);
		result.put(YcContext.DATA, "");
		return result;
	}
	
	
	/**
	 * copyrightSuccess:(推送作品保权成功). <br/> 
	 *
	 * @author qinhaiguan
	 * @param workid  作品id
	 * @param uid  用户id
	 * @param type  作品类型  1歌曲 2歌词
	 * @param result
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public Map<String,Object> copyrightSuccess(Long workid,Long uid, Integer type,String orderNo,Map<String,Object> result) throws Exception {

		//***********增加推送部分****************
		//1歌曲 2歌词 3保权词曲
		if (1==type||3==type) {
			WorkOpenDetail work=workCacheMapper.getWorOpenDetailById(workid);
			logger.info("开始推送歌曲保权消息>>>>>>推送给用户id:"+uid+",作品名："+work.getTitle()+",订单号："+orderNo);
			//针对一个人一个作品只第一次的时候推送
			int pushCount=pushMessageFlowMapper.getCountPushMessage(null,uid, workid, 1, YtConstant.PUSH_TYPE_COPYRIGHSUCCESS);
			String msgIdAnd=null;
			String msgIdIOS=null;
			YcPushMassageFlow flow =new YcPushMassageFlow();
			if (0==pushCount) {
				//推送给android平台
				msgIdAnd=this.pushCopyrightSuccessAndroid(String.valueOf(uid), work.getTitle(),orderNo);
				if (null!=msgIdAnd) {
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_ANDROID);
					flow.setMsgid(msgIdAnd);
				}
				//推送给IOS平台
				msgIdIOS=this.pushCopyrightSuccessIOS(String.valueOf(uid), work.getTitle(),orderNo);
				if (null!=msgIdIOS) {
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_IOS);
					flow.setMsgid(msgIdIOS);
				}
				
				if (null==msgIdAnd&&null==msgIdIOS) {
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_NULL);
					flow.setMsgid(YtConstant.JPUSH_KEY_MSGID_NULL);
					
				}
			}
			//记录推送流水
			flow.setOtheruid(uid);
			flow.setWorkid(workid);
			flow.setWorktype(type);//1歌曲 2歌词 3词曲
			flow.setOrderNo(orderNo);//订单号
			flow.setPushtype(YtConstant.PUSH_TYPE_COPYRIGHSUCCESS);
			flow.setContent(YtConstant.PUSH_TITLE_COPYRIGHSUCCESS.replaceAll("W", work.getTitle()));
			pushMessageFlowMapper.savePushMessageFlow(flow);
		}else{
			MusicLyrics  lyrics=lyricsMapper.getShareLyricById(workid);
			logger.info("开始推送歌词保权消息>>>>>>推送给用户id:"+uid+",作品名："+lyrics.getTitle());
			//针对一个人一个作品只第一次的时候推送
			int pushCount=pushMessageFlowMapper.getCountPushMessage(null,uid, workid, 2, YtConstant.PUSH_TYPE_COPYRIGHSUCCESS);
			String msgIdAnd=null;
			String msgIdIOS=null;
			YcPushMassageFlow flow =new YcPushMassageFlow();
			if (0==pushCount) {
				//推送给android平台
				msgIdAnd=this.pushCopyrightSuccessAndroid(String.valueOf(uid), lyrics.getTitle(),orderNo);
				if (null!=msgIdAnd) {
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_ANDROID);
					flow.setMsgid(msgIdAnd);
				}
				//推送给IOS平台
				msgIdIOS=this.pushCopyrightSuccessIOS(String.valueOf(uid), lyrics.getTitle(),orderNo);
				if (null!=msgIdIOS) {
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_IOS);
					flow.setMsgid(msgIdIOS);
				}
				
				if (null==msgIdAnd&&null==msgIdIOS) {
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_NULL);
					flow.setMsgid(YtConstant.JPUSH_KEY_MSGID_NULL);
					
				}
			}

			//记录推送流水
			flow.setOtheruid(uid);
			flow.setWorkid(workid);
			flow.setWorktype(type);//1歌曲 2歌词
			flow.setOrderNo(orderNo);//订单号
			flow.setPushtype(YtConstant.PUSH_TYPE_COPYRIGHSUCCESS);
			
			flow.setContent(YtConstant.PUSH_TITLE_COPYRIGHSUCCESS.replaceAll("W", lyrics.getTitle()));
			pushMessageFlowMapper.savePushMessageFlow(flow);
		}
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YcContext.EMPTY_STRING);
		result.put(YcContext.DATA, "");
		return result;
	}
	
	private String pushCopyrightSuccessAndroid(String uid,String title,String orderNo){
		logger.info("开始推保权成功消息Android>>>>>>推送给用户id:"+uid+",作品名:"+title+",订单号："+orderNo);
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("type", YtConstant.PUSH_TYPE_COPYRIGHSUCCESS);
		param.put("orderNo", orderNo);
		param.put("title",YtConstant.YINCHAO);
		param.put("content",YtConstant.PUSH_TITLE_COPYRIGHSUCCESS.replaceAll("W", title));
		String pushMessage=JSONObject.toJSONString(param);
		PushResult re=YcPush.ycSendPushAndroid(uid, pushMessage);
		logger.info("开始推保权成功消息返回结果Android:"+re);
		String msgId=null;
		if (null!=re) {
			msgId=JSONObject.parseObject(JSONObject.toJSONString(re)).getString(YtConstant.JPUSH_KEY_MSGID);
		}
		return msgId;
	}
	
	
	/**
	 * copyrightFail:(推送作品保权失败). <br/> 
	 *
	 * @author qinhaiguan
	 * @param workid  作品id
	 * @param uid  用户id
	 * @param type  作品类型  1歌曲 2歌词
	 * @param result
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public Map<String,Object> copyrightFail(Long workid,Long uid, Integer type,String orderNo,Map<String,Object> result) throws Exception {

		//***********增加推送部分****************
		//1歌曲 2歌词 3词曲
		if (1==type||3==type) {
			WorkOpenDetail work=workCacheMapper.getWorOpenDetailById(workid);
			logger.info("开始推送歌曲保权失败消息>>>>>>推送给用户id:"+uid+",作品名："+work.getTitle()+",订单号："+orderNo);
			//针对一个人一个作品只第一次的时候推送
			int pushCount=pushMessageFlowMapper.getCountPushMessage(null,uid, workid, 1, YtConstant.PUSH_TYPE_COPYRIGHFAIL);
			String msgIdAnd=null;
			String msgIdIOS=null;
			YcPushMassageFlow flow =new YcPushMassageFlow();
			if (0==pushCount) {
				//推送给android平台
				msgIdAnd=this.pushCopyrightFailAndroid(String.valueOf(uid), work.getTitle(),orderNo);
				if (null!=msgIdAnd) {
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_ANDROID);
					flow.setMsgid(msgIdAnd);
				}
				//推送给IOS平台
				msgIdIOS=this.pushCopyrightFailIOS(String.valueOf(uid), work.getTitle(),orderNo);
				if (null!=msgIdIOS) {
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_IOS);
					flow.setMsgid(msgIdIOS);
				}
				
				if (null==msgIdAnd&&null==msgIdIOS) {
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_NULL);
					flow.setMsgid(YtConstant.JPUSH_KEY_MSGID_NULL);
					
				}
			}
			//记录推送流水
			flow.setOtheruid(uid);
			flow.setWorkid(workid);
			flow.setWorktype(type);//1歌曲 2歌词 3词曲
			flow.setOrderNo(orderNo);//订单号
			flow.setPushtype(YtConstant.PUSH_TYPE_COPYRIGHFAIL);
			flow.setContent(YtConstant.PUSH_TITLE_COPYRIGHFAIL.replaceAll("W", work.getTitle()));
			pushMessageFlowMapper.savePushMessageFlow(flow);
		}else{
			MusicLyrics  lyrics=lyricsMapper.getShareLyricById(workid);
			logger.info("开始推送歌词保权失败消息>>>>>>推送给用户id:"+uid+",作品名："+lyrics.getTitle()+",订单号："+orderNo);
			//针对一个人一个作品只第一次的时候推送
			int pushCount=pushMessageFlowMapper.getCountPushMessage(null,uid, workid, 2, YtConstant.PUSH_TYPE_COPYRIGHFAIL);
			String msgIdAnd=null;
			String msgIdIOS=null;
			YcPushMassageFlow flow =new YcPushMassageFlow();
			if (0==pushCount) {
				//推送给android平台
				msgIdAnd=this.pushCopyrightFailAndroid(String.valueOf(uid), lyrics.getTitle(),orderNo);
				if (null!=msgIdAnd) {
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_ANDROID);
					flow.setMsgid(msgIdAnd);
				}
				//推送给IOS平台
				msgIdIOS=this.pushCopyrightFailIOS(String.valueOf(uid), lyrics.getTitle(),orderNo);
				if (null!=msgIdIOS) {
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_IOS);
					flow.setMsgid(msgIdIOS);
				}
				
				if (null==msgIdAnd&&null==msgIdIOS) {
					flow.setPlatform(YtConstant.JPUSH_PLATFORM_NULL);
					flow.setMsgid(YtConstant.JPUSH_KEY_MSGID_NULL);
					
				}
			}

			//记录推送流水
			flow.setOtheruid(uid);
			flow.setWorkid(workid);
			flow.setWorktype(type);//1歌曲 2歌词
			flow.setOrderNo(orderNo);//订单号
			flow.setPushtype(YtConstant.PUSH_TYPE_COPYRIGHFAIL);
			
			flow.setContent(YtConstant.PUSH_TITLE_COPYRIGHFAIL.replaceAll("W", lyrics.getTitle()));
			pushMessageFlowMapper.savePushMessageFlow(flow);
		}
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YcContext.EMPTY_STRING);
		result.put(YcContext.DATA, "");
		return result;
	}
	
	private String pushCopyrightFailAndroid(String uid,String title,String orderNo){
		logger.info("开始推保权失败消息Android>>>>>>推送给用户id:"+uid+",作品名:"+title);
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("type", YtConstant.PUSH_TYPE_COPYRIGHFAIL);
		param.put("title",YtConstant.YINCHAO);
		param.put("content",YtConstant.PUSH_TITLE_COPYRIGHFAIL.replaceAll("W", title));
		String pushMessage=JSONObject.toJSONString(param);
		PushResult re=YcPush.ycSendPushAndroid(uid, pushMessage);
		logger.info("开始推保权失败消息返回结果Android:"+re);
		String msgId=null;
		if (null!=re) {
			msgId=JSONObject.parseObject(JSONObject.toJSONString(re)).getString(YtConstant.JPUSH_KEY_MSGID);
		}
		return msgId;
	}
	
	public Map<String,Object> demangExpire(Long uid,Long did,Map<String,Object> result) throws Exception {

		
		
		logger.info("开始需求活动到期消息>>>>>>推送给用户id:"+uid);
		String msgIdAnd=null;
		String msgIdIOS=null;
		YcPushMassageFlow flow =new YcPushMassageFlow();
		//推送给android平台
		
		msgIdAnd=this.pushDemandExpireAndroid(String.valueOf(uid), did);
		if (null!=msgIdAnd) {
			flow.setPlatform(YtConstant.JPUSH_PLATFORM_ANDROID);
			flow.setMsgid(msgIdAnd);
		}
		//推送给IOS平台
		msgIdIOS=this.pushDemandExpireIOS(String.valueOf(uid), did);
		if (null!=msgIdIOS) {
			flow.setPlatform(YtConstant.JPUSH_PLATFORM_IOS);
			flow.setMsgid(msgIdIOS);
		}
		
		if (null==msgIdAnd&&null==msgIdIOS) {
			flow.setPlatform(YtConstant.JPUSH_PLATFORM_NULL);
			flow.setMsgid(YtConstant.JPUSH_KEY_MSGID_NULL);
			
		}
		
		//记录推送流水
		flow.setOtheruid(uid);
		flow.setWorktype(3);
		flow.setWorkid(did);
		flow.setPushtype(YtConstant.PUSH_TYPE_COOPERATE_EXPIRE);
		flow.setContent(YtConstant.PUSH_TITLE_COOPERATE_EXPIRE);
		pushMessageFlowMapper.savePushMessageFlow(flow);
		
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YcContext.EMPTY_STRING);
		result.put(YcContext.DATA, "");
		return result;
	}
	
	private String pushDemandExpireAndroid(String uid,long did){
		logger.info("开始需求活动到期消息Android>>>>>>推送给用户id:"+uid);
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("type", YtConstant.PUSH_TYPE_COOPERATE_EXPIRE);
		param.put("title",YtConstant.YINCHAO);
		param.put("did", did);
		param.put("content",YtConstant.PUSH_TITLE_COOPERATE_EXPIRE);
		String pushMessage=JSONObject.toJSONString(param);
		PushResult re=YcPush.ycSendPushAndroid(uid, pushMessage);
		logger.info("开始推保需求活动到期返回结果Android:"+re);
		String msgId=null;
		if (null!=re) {
			msgId=JSONObject.parseObject(JSONObject.toJSONString(re)).getString(YtConstant.JPUSH_KEY_MSGID);
		}
		return msgId;
	}
	
	private String pushDemandExpireIOS(String uid,long did){
		logger.info("开始推送需求活动到期消息IOS>>>>>>推送给用户id:"+uid);
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("type", YtConstant.PUSH_TYPE_COOPERATE_EXPIRE);
		param.put("title",YtConstant.YINCHAO);
		param.put("did", did);
		param.put("content",YtConstant.PUSH_TITLE_COOPERATE_EXPIRE);
		String pushMessage=JSONObject.toJSONString(param);
		PushResult re=YcPush.ycSendPushCooperateIOS(uid, pushMessage);
		logger.info("推送需求活动到期返回结果IOS:"+re);
		String msgId=null;
		if (null!=re) {
			msgId=JSONObject.parseObject(JSONObject.toJSONString(re)).getString(YtConstant.JPUSH_KEY_MSGID);
		}
		return msgId;
	}
}
